program PermGen;

const
   N = 3;

var
   p : array[1..N] of integer;

procedure Exch(i, j : integer);
var
   t :  integer;
begin
   t := p[i];
   p[i] := p[j];
   p[j] := t;
end; { Exch }

procedure Print();
var
   i : integer;
begin
   for i := 1 to N do
      write(p[i],' ');
   writeln();
end; { Print }

procedure Generate(eN : integer);
var
   i : integer;
begin
   if eN = 1 then Print();
   for i := 1 to eN do
   begin
      Exch(i, eN);
      Generate(eN-1);
      Exch(i, eN);
   end;
end; { Generate }

var
   i : integer;

begin
   for i:= 1 to N do p[i] := i;
   Generate(N);
end.
